//
//  DVActivityVCViewController.m
//  Demo CMTabBarController
//
//  Created by pan Shiyu on 13-4-25.
//  Copyright (c) 2013年 Team Force LLC. All rights reserved.
//

#import "DVActivityVC.h"
#import "DVSetting.h"
#import "DVActivity.h"
#import "DVActData.h"

@implementation DVActivityVC{
    UITextField *_actTitle;
    UILabel *_timeLabel;
    UISlider *_timeSlider;
    
    UIButton *_addButton;
}

- (id)initWithActivity:(DVActivity*)tmpActivity mode:(ViewMode)mode {
    self = [super init];
    if (self) {
        _viewMode = mode;
        _activity = tmpActivity;
        if (mode == ViewModeCreate) {
            _activity = [[DVActivity alloc]init];
        }
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	self.title = @"发起活动";
    
    _actTitle = [[UITextField alloc] initWithFrame:CGRectMake(20.0f, 11.0f, 280.0f, 45.0f)];
    _actTitle.returnKeyType = UIReturnKeyDone;
    _actTitle.enablesReturnKeyAutomatically = YES;
    _actTitle.autocapitalizationType = UITextAutocapitalizationTypeNone;
    _actTitle.autocorrectionType = UITextAutocorrectionTypeNo;
    _actTitle.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _actTitle.placeholder = @"输入活动内容";
    //    _actTitle.insets = UIEdgeInsetsMake(0, 45, 0, 10);
    _actTitle.backgroundColor = [UIColor lightTextColor];
    _actTitle.font = Font(14);
    [self.view addSubview:_actTitle];
    
    _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 280, 18)];
    _timeLabel.backgroundColor = [UIColor clearColor];
    _timeLabel.font = Font(14);
    [self.view addSubview:_timeLabel];
    
    _timeSlider = [[UISlider alloc] initWithFrame:CGRectMake(20, 120, 280, 20)];
    _timeSlider.minimumValue = 0;
    _timeSlider.maximumValue = 12000;
    //假设最小单位是10分钟，最大是三个月， 三个月是3*30*24*6
    [_timeSlider addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_timeSlider];
    
    UIButton *cmdButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    cmdButton.frame = CGRectMake(110, 180, 100, 30);
    [cmdButton addTarget:self action:@selector(onCmd) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:cmdButton];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    [tap addTarget:self action:@selector(hideKeyBoard)];
    [self.view addGestureRecognizer:tap];
    
    if (_viewMode != ViewModeCreate) {
        _actTitle.text = _activity.title;
        _timeLabel.text = [_activity.orderedTime description];
    } else {
        _timeLabel.text = [[NSDate date] description];
    }
}

- (NSDate*)dateBySliderValue:(int) value{
    int secs = value * 10 * 60;//秒
    return [NSDate dateWithTimeIntervalSinceNow:secs];
}

- (void)valueChanged:(UISlider *)sender {
    NSDate *newDate = [self dateBySliderValue:sender.value];
    _timeLabel.text = [newDate description];
}

- (void)hideKeyBoard {
    [_actTitle resignFirstResponder];
}

- (void)onCmd {
    if (_viewMode == ViewModeNone) {
        return;
    }
    
    _activity.title = _actTitle.text;
    _activity.orderedTime = [self dateBySliderValue:_timeSlider.value];
    _activity.content = nil;
    _activity.hasNews = NO;
    _activity.type = DVActivityTypeDealed;
    
    if (_viewMode == ViewModeCreate) {
        [[DVActData sharedActData].actList addObject:_activity];
    }
    
}


@end
